<!DOCTYPE html>
<html>
<head>
<title>oops!homework3--filters(ringbuffer).md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84%E6%BB%A4%E6%B3%A2%E9%A2%84%E5%A4%84%E7%90%86c%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B">心电信号的滤波预处理——C语言编程</h1>
<h2 id="%E7%AC%AC%E4%B8%89%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第三次作业）</h2>
<h2 id="%E4%B8%80%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E4%BD%8E%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%92%8C%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2%E7%9A%84%E7%A8%8B%E5%BA%8F">一、对心电信号进行低通滤波和高通滤波的程序</h2>
<h3 id="1%E6%BB%A4%E6%B3%A2">1.滤波</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"stdio.h"</span></span>

<span class="hljs-comment">//y(n)=2y(n-1)-y(n-2)+x(n)-2x(n-6)+x(n-12)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1,yn2,i,n,xn6,xn12=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a[<span class="hljs-number">13</span>]={<span class="hljs-number">0</span>};

    <span class="hljs-comment">//环形缓冲区</span>
    <span class="hljs-keyword">if</span>(n&gt;=<span class="hljs-number">6</span>)
        xn6 = a[n<span class="hljs-number">-6</span>];
    <span class="hljs-keyword">else</span>
        xn6 = a[n+<span class="hljs-number">7</span>]; <span class="hljs-comment">// xn6 = a[13-(6-n)]</span>
    <span class="hljs-keyword">if</span>(n&lt;<span class="hljs-number">12</span>)
        xn12 = a[n+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn12 = a[13-(12-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn12 = a[<span class="hljs-number">0</span>];

    a[n] = x;
    n++;
    <span class="hljs-keyword">if</span>(n==<span class="hljs-number">13</span>)
        n=<span class="hljs-number">0</span>;
    
    <span class="hljs-comment">//循环方式</span>
    <span class="hljs-comment">/*
    for(i=12; i&gt;0; i--)
    {
        a[i] = a[i-1];
    }
    a[0] = x;
    */</span>
    <span class="hljs-keyword">int</span> y = <span class="hljs-number">2</span>*yn1 - yn2 + x <span class="hljs-number">-2</span>*xn6 + xn12;
    yn2 = yn1;
    yn1 = y;

    y = y/<span class="hljs-number">36</span>; <span class="hljs-comment">//除去36倍低通滤波增益</span>
    <span class="hljs-keyword">return</span> y;
}

<span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32),z(n)=x(n-16)-y(n)/32 OR y(nT)=32x(nT-16T)-[y(nT-T)+x(nT)-x(nT-32T)]</span>
<span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">hpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1,xn32,xn16,i,n=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a[<span class="hljs-number">33</span>]={<span class="hljs-number">0</span>},b[<span class="hljs-number">17</span>]={<span class="hljs-number">0</span>};

    <span class="hljs-comment">//环形缓冲区</span>
    <span class="hljs-keyword">if</span>(n&lt;<span class="hljs-number">32</span>)
        xn32 = a[n+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn32 = a[32-(32-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn32 = a[<span class="hljs-number">0</span>];
    a[n] = x;
    n++;
    <span class="hljs-keyword">if</span>(n==<span class="hljs-number">33</span>)
        n=<span class="hljs-number">0</span>;

    <span class="hljs-keyword">if</span>(i&lt;<span class="hljs-number">16</span>)
        xn16 = b[i+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn16 = a[16-(16-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn16 = b[<span class="hljs-number">0</span>];
    b[i] = x;
    i++;
    <span class="hljs-keyword">if</span>(i==<span class="hljs-number">17</span>)
        i=<span class="hljs-number">0</span>;

    <span class="hljs-comment">//循环方式实现</span>
    <span class="hljs-comment">/*
    for(i=32; i&gt;0; i--)
    {
        a[i] = a[i-1];
    }
    a[0] = x;

    for(n=16; n&gt;0; n--)
    {
        b[n] = b[n-1];
    }
    b[0] = x;
    */</span>

    <span class="hljs-keyword">int</span> y = yn1 + x -xn32;
    yn1 = y;
    <span class="hljs-keyword">int</span> z = xn16 - y/<span class="hljs-number">32</span> ;  <span class="hljs-comment">//除去32倍的高通滤波增益</span>
    
    <span class="hljs-keyword">return</span> z;
}
</div></code></pre>
<h3 id="2%E4%B8%BB%E7%A8%8B%E5%BA%8F">2.主程序</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>


<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_lp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat"</span>,<span class="hljs-string">"wb"</span>);  <span class="hljs-comment">//只写模式</span>
    FILE *fp_hp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat"</span>,<span class="hljs-string">"wb"</span>);
    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        short y = lpfilter(x); <span class="hljs-comment">//逐点低通滤波</span>
        short z = hpfilter(y); <span class="hljs-comment">//逐点高通滤波</span>
        fwrite(&amp;y,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_lp);  <span class="hljs-comment">//将文件每两字节一个数据写入文件中</span>
        fwrite(&amp;z,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_hp);  
        <span class="hljs-comment">//printf("%d%d \r\n",i,x);</span>
    }
    
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_lp);
    fclose(fp_hp);
    
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;

}
</div></code></pre>
<h2 id="%E4%BA%8C%E4%BD%BF%E7%94%A8matlab%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6%E5%B9%B6%E6%98%BE%E7%A4%BA">二、使用MatLab读取文件并显示</h2>
<h3 id="1%E7%A8%8B%E5%BA%8F">1.程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>

fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">figure</span>;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-r'</span>);
fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-b'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-g'</span>);
<span class="hljs-built_in">legend</span>(<span class="hljs-string">'原始'</span>,<span class="hljs-string">'低通'</span>,<span class="hljs-string">'高通'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'对比'</span>);

<span class="hljs-built_in">figure</span>;
subplot(<span class="hljs-number">311</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'原始'</span>);
subplot(<span class="hljs-number">312</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'低通'</span>);
subplot(<span class="hljs-number">313</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'再高通'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="没有对比就没有伤害.png" alt="">
<img src="诶这才是真的对比.png" alt=""></p>

</body>
</html>
